﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Leap;
using System.Diagnostics;

namespace LeapInterface
{
    public partial class Form1 : Form
    {
        
        bool fingersStatus = false;

        private static LeapListener listener;
        private static Controller controller;

        Thread workingThread;

        public Form1()
        {
            InitializeComponent();
            Logger.dataEvent += new Logger.DataEventHandler(SetTextBoxConsole);
            
            Load += new EventHandler(Form1_Load);

            workingThread = new Thread(createLeapListener);
            workingThread.IsBackground = true;
            //workingThread.Priority = ThreadPriority.AboveNormal;
            //workingThread.Start();

            this.FormClosed += MyClosedHandler;
            //this.TopMost = true;
            timer();

            this.textBox2.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox8.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox10.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox11.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox12.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox13.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox14.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox15.KeyDown += new KeyEventHandler(KeyDownFilter);
            this.textBox16.KeyDown += new KeyEventHandler(KeyDownFilter);

            /*
            this.textBox2.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox8.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox10.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox11.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox12.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox13.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox14.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox15.KeyDown += new KeyEventHandler(textBox_KeyPress);
            this.textBox16.KeyDown += new KeyEventHandler(textBox_KeyPress);
             * */

  

            
            //backgroundWorker1.RunWorkerAsync("begin");
            radioButton1.Checked = true;
        }

        public static void createLeapListener()
        {
            if (!MyGlobals.startThread)
            {
                listener = new LeapListener();
                controller = new Controller();
                controller.SetPolicyFlags(Leap.Controller.PolicyFlag.POLICYBACKGROUNDFRAMES);
                //LeapController leapController = new LeapController(listener, controller);
                //controller.AddListener(listener);
                //controller.AddListener(new DummyListener());
                MyGlobals.startThread = true;
                //Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
                //stopwatch.Start();
                
            }

            while (MyGlobals.startThread)
            {
                if (MyGlobals.processOn)
                {

                    listener.OnFrame(controller, controller.Frame());
                    //Console.WriteLine("thread");
                    //System.Threading.Thread.Sleep(30);

                }

            }
        }

        protected void MyClosedHandler(object sender, EventArgs e)
        {
            workingThread.Abort();
        }


        public void refreshDiffText(object sender, ElapsedEventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<object, ElapsedEventArgs>(refreshDiffText), new object[] { sender, e });
                return;
            }

            //label12.ForeColor = Color.LightSteelBlue; 
        }

        private void timer()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(refreshDiffText);
            // check every 1 minuut
            timer.Interval = 200;
            timer.Enabled = true;
            timer.Start();
        }

        private void ResetLabelGesture()
        {
            label10.ForeColor = Color.LightSteelBlue;
            label11.ForeColor = Color.LightSteelBlue;
            label12.ForeColor = Color.LightSteelBlue;
            label13.ForeColor = Color.LightSteelBlue;
            label14.ForeColor = Color.LightSteelBlue;
            label15.ForeColor = Color.LightSteelBlue;
            label16.ForeColor = Color.LightSteelBlue;
            label17.ForeColor = Color.LightSteelBlue;
            label20.ForeColor = Color.LightSteelBlue;
        }

        public void SetTextBoxConsole(object sender, DataEventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<object, DataEventArgs>(SetTextBoxConsole), new object[] { sender, e });
                return;
            }

            if (e.Property == "Ready")
            {
                //System.Threading.Thread.Sleep(100);
            }
            else if (e.Property == "Fps")
            {
                textBox1.Text = ""+MyGlobals.fps;
            }
            else if (e.Property == "Done")
            {
                //IncrementDataNumber();
                //System.Threading.Thread.Sleep(100);
                //MessageBox.Show("id = " + MyGlobals.currentGestureId);

                ResetLabelGesture();

                if (MyGlobals.gestureSet[0] == true)
                {
                    label10.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[1] == true)
                {
                    label11.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[2] == true)
                {
                    label12.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[3] == true)
                {
                    label13.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[4] == true)
                {
                    label14.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[5] == true)
                {
                    label15.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[6] == true)
                {
                    label16.ForeColor = Color.BlueViolet;
                }

                if (MyGlobals.gestureSet[7] == true)
                {
                    label17.ForeColor = Color.BlueViolet;
                }
                if (MyGlobals.gestureSet[8] == true)
                {
                    label20.ForeColor = Color.BlueViolet;
                }

                
                

                //instance.SaveInfoFile();
               
            }
             
            else if (e.Property == "Gained focus on 5 fingers")
            {
                
                //if (pictureBox1.Image != green)
                //pictureBox1.Image = green;
                pictureBox1.Image = Image.FromFile("green.png");
                pictureBox2.Image = Image.FromFile("red.png");
                pictureBox3.Image = Image.FromFile("red.png");
                fingersStatus = true;
                //System.Threading.Thread.Sleep(100);
            }

            else if (e.Property == "Gained focus on 2 fingers")
            {

                //if (pictureBox1.Image != green)
                //pictureBox1.Image = green;
                pictureBox1.Image = Image.FromFile("red.png");
                pictureBox2.Image = Image.FromFile("green.png");
                pictureBox3.Image = Image.FromFile("red.png");
                fingersStatus = true;
                //System.Threading.Thread.Sleep(100);
            }

            else if (e.Property == "Gained focus on 3 fingers")
            {

                //if (pictureBox1.Image != green)
                //pictureBox1.Image = green;
                pictureBox1.Image = Image.FromFile("red.png");
                pictureBox2.Image = Image.FromFile("red.png");
                pictureBox3.Image = Image.FromFile("green.png");
                fingersStatus = true;
                //System.Threading.Thread.Sleep(100);
            }
            else if (e.Property == "Lost focus on fingers" || e.Property == "Refresh focus on 5 fingers")
            {
                //if(pictureBox1.Image != red)
                //pictureBox1.Image = red;
                pictureBox1.Image = Image.FromFile("red.png");
                pictureBox2.Image = Image.FromFile("red.png");
                pictureBox3.Image = Image.FromFile("red.png");
                fingersStatus = false;
                //System.Threading.Thread.Sleep(100);
            }
 

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
            IEnumerator enu = listBox1.Items.GetEnumerator();
            List<string> list = new List<string>();

            while (enu.MoveNext())
            {
                list.Add((string)enu.Current);
            }

            String strS = (string)listBox1.SelectedItem;

            //MessageBox.Show("test : " + strS);

            foreach(String str in list)
            {

                if (str == strS)
                {
                    if (str == "No gesture")
                    {
                        MyGlobals.currentGestureId = -1;
                        return;
                    }
                    else
                    {
                        ItemsDescription.Clear();
                        foreach (Info i in MyGlobals.infoList)
                            if (i.name == str)
                            {
                                ItemsDescription.Add("id = " + i.id);
                                ItemsDescription.Add("number of data = " + i.dataNumber);
                                listBox2.Items.Clear();
                                foreach(string s in ItemsDescription)
                                {
                                    listBox2.Items.Add(s);
                                }
                                //listBox2.DataSource = ItemsDescription;
                                listBox2.SetSelected(0, true);
                                MyGlobals.currentGestureId = i.id;
                                return;
                            }
                        return;
                    }
                  
                }
                   
            }
             * */
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

      
        private void button1_Click(object sender, EventArgs e)
        {
            /*
            if (MyGlobals.dataFileName == "" || MyGlobals.infoFileName == "" || fingersStatus == false)
                MessageBox.Show("No chosen data file");

            else if (textBox1.TextLength > 0 && !containsName(textBox1.Text))
            {
                //MessageBox.Show("name : " + textBox1.Text);
                AddNewGesture(textBox1.Text);
                textBox1.Text = "";
                listBox1.SetSelected(listBox1.Items.Count - 1, true);
                listBox2.SetSelected(listBox2.Items.Count - 1, true);
 
            }
            else
                MessageBox.Show("wrong name or description");
             * */
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //gesture name
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //description 
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (workingThread.IsAlive && !MyGlobals.processOn)
            {
                MyGlobals.processOn = true;
            }
            else if (!MyGlobals.processOn)
            {
                MyGlobals.processOn = true;
                //MyGlobals.startThread = true;
                workingThread.Start();
            }
            /*
            if (instance == null)
            {
                instance = DataIOSingleton.Instance;
                //instance.ScanInfoFile();
                //GetAllGesture();
                //GetGestureDescription();
                //workingThread.Start();
                MyGlobals.processOn = true;
            }
            else
            {
                //instance.UpdateFiles();
                //GetAllGesture();
                //GetGestureDescription();
            }

            //MyGlobals.startThread = true;

            */
            
                //MyGlobals.gatherData = !MyGlobals.gatherData;
        }


        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //instance.SaveInfoFile();
            this.Close();
        }

        private void LoadData()
        {
            //instance.scanDataFileForLine();
        }

        private void CreateData()
        {

            //file.WriteLine(MyGlobals.fileLength+" "+MyGlobals.lineSize+" "+ MyGlobals.numberResult+ Environment.NewLine);
        }


        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open data file";
            fDialog.Filter = "TEXT Files|*.txt|DATA Files|*.data";
            fDialog.InitialDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //fDialog.Multiselect = true;

            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show(fDialog.FileName.ToString());
                MyGlobals.dataFileName = fDialog.FileName.ToString();
                textBox5.Text = MyGlobals.dataFileName;

                if (MyGlobals.dataFileName != "" && MyGlobals.infoFileName != "")
                    if (instance == null)
                        instance = DataIOSingleton.Instance;
                    else
                        instance.UpdateFiles();
                 
                //LoadData();
            }
             * */
        }

        private void button3_Click(object sender, EventArgs e)
        {
            /*
            if (textBox5.Text == "")
            {
                MessageBox.Show("Please enter the data file first");
                return;
            }

            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open info file";
            fDialog.Filter = "TEXT Files|*.txt";
            fDialog.InitialDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //fDialog.Multiselect = true;

            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show(fDialog.FileName.ToString());
                MyGlobals.infoFileName = fDialog.FileName.ToString();
                textBox2.Text = MyGlobals.infoFileName;
                
                 if (MyGlobals.dataFileName != "" && MyGlobals.infoFileName != "")
                    if (instance == null)
                        instance = DataIOSingleton.Instance;
                    else
                        instance.UpdateFiles();

                instance.ScanInfoFile();
                //listBox2.DataSource = ItemsDescription;
                //listBox1.DataBindings.Clear();
                //listBox2.DataBindings.Clear();
                GetAllGesture();
                //listBox1.DataSource = Items;
                GetGestureDescription();
                //listBox1.DataSource = Items;
                //listBox2.DataSource = ItemsDescription;
                //listBox1.SetSelected(listBox1.Items.Count - 1, true);
                //listBox2.SetSelected(listBox2.Items.Count - 1, true);
                listBox1.Items.Clear();
                foreach (string s in Items)
                {
                    listBox1.Items.Add(s);
                }
                listBox1.SetSelected(0, true);
                
            }
             */ 
        }
/*
        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void GetAllGesture() {

            //Items.Clear();

            if (MyGlobals.infoList.Count == 0)
            {
                Items.Add("No gesture");
                MessageBox.Show(" "+MyGlobals.infoList.Count);
            }
            else
            {

                foreach (Info i in MyGlobals.infoList)
                {
                    //MessageBox.Show("test " + i.name);
                    //Console.WriteLine("test " + i.name);
                    Items.Add(i.name);
                   
                }

                
            }



        }

        private void GetGestureDescription()
        {
            ItemsDescription.Clear();

            if (MyGlobals.infoList.Count == 0)
                ItemsDescription.Add("No Description");
            else
                ItemsDescription.Add("Click on a gesture for a description");

            //listBox2.DataSource = ItemsDescription;
        }

        private void AddNewGesture(string name)
        {


            //MessageBox.Show(name + " file = " + MyGlobals.dataFileName + " l = " + 0);
            //instance.AppendInfoText(name);
            Info i;
            //MessageBox.Show("number " + MyGlobals.gestureIdNumber);
            if (Items[0] == "No gesture")
                i.id = MyGlobals.gestureIdNumber++;
            else
                i.id = MyGlobals.gestureIdNumber++;
            
            i.name = name;
            i.dataNumber = 0;
            MyGlobals.infoList.Add(i);
            GetAllGesture();
            instance.SaveInfoFile();
        }
*/
        private void UpdateSelectedGesture()
        {
            /*
            listBox1.SetSelected(listBox1.Items.Count - 1, true);
            listBox2.SetSelected(listBox2.Items.Count - 1, true);
            MyGlobals.currentGestureId = ((Info)(listBox1.Items[listBox1.Items.Count - 1])).id;
        */
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            MyGlobals.refreshStatusFingers = true;
            MyGlobals.staticFrame = false;
            Logger.OnPropertyChange(this, new DataEventArgs("Refresh focus on 5 fingers"));

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void label2_Click_2(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized) this.Hide();
            
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
  
        }

        private void resetModifier()
        {
            for (int i = 0; i < 9; i++)
            {
                if (MyGlobals.ctrl[i])
                {
                    Keyboard.KeyRelease(i);
                }

                else if (MyGlobals.alt[i])
                {
                    Keyboard.KeyRelease(i);
                }
            

                else if (MyGlobals.shift[i])
                {
                    Keyboard.KeyRelease(i);
                }
            
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            MyGlobals.startThread = false;
            resetModifier();
            workingThread.Abort();
            Application.Exit();
        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged_2(object sender, EventArgs e)
        {
             
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void handleCtrl(TextBox sender)
        {
            if (sender.Equals(textBox2))
            {
                MyGlobals.ctrl[0] = true;

            }
            else if (sender.Equals(textBox8))
            {
                MyGlobals.ctrl[1] = true;
            }
            else if (sender.Equals(textBox10))
            {
                MyGlobals.ctrl[2] = true;
            }
            else if (sender.Equals(textBox11))
            {
                MyGlobals.ctrl[3] = true;
            }
            else if (sender.Equals(textBox12))
            {
                MyGlobals.ctrl[4] = true;
            }
            else if (sender.Equals(textBox13))
            {
                MyGlobals.ctrl[5] = true;
            }
            else if (sender.Equals(textBox14))
            {
                MyGlobals.ctrl[6] = true;
            }
            else if (sender.Equals(textBox15))
            {
                MyGlobals.ctrl[7] = true;
            }
            else if (sender.Equals(textBox16))
            {
                MyGlobals.ctrl[8] = true;
            }
        }

        private void handleAlt(TextBox sender)
        {
            if (sender.Equals(textBox2))
            {
                MyGlobals.alt[0] = true;

            }
            else if (sender.Equals(textBox8))
            {
                MyGlobals.alt[1] = true;
            }
            else if (sender.Equals(textBox10))
            {
                MyGlobals.alt[2] = true;
            }
            else if (sender.Equals(textBox11))
            {
                MyGlobals.alt[3] = true;
            }
            else if (sender.Equals(textBox12))
            {
                MyGlobals.alt[4] = true;
            }
            else if (sender.Equals(textBox13))
            {
                MyGlobals.alt[5] = true;
            }
            else if (sender.Equals(textBox14))
            {
                MyGlobals.alt[6] = true;
            }
            else if (sender.Equals(textBox15))
            {
                MyGlobals.alt[7] = true;
            }
            else if (sender.Equals(textBox16))
            {
                MyGlobals.alt[8] = true;
            }
        }

        private void handleShift(TextBox sender)
        {
            if (sender.Equals(textBox2))
            {
                MyGlobals.shift[0] = true;
            }
            else if (sender.Equals(textBox8))
            {
                MyGlobals.shift[1] = true;
            }
            else if (sender.Equals(textBox10))
            {
                MyGlobals.shift[2] = true;
            }
            else if (sender.Equals(textBox11))
            {
                MyGlobals.shift[3] = true;
            }
            else if (sender.Equals(textBox12))
            {
                MyGlobals.shift[4] = true;
            }
            else if (sender.Equals(textBox13))
            {
                MyGlobals.shift[5] = true;
            }
            else if (sender.Equals(textBox14))
            {
                MyGlobals.shift[6] = true;
            }
            else if (sender.Equals(textBox15))
            {
                MyGlobals.shift[7] = true;
            }
            else if (sender.Equals(textBox16))
            {
                MyGlobals.shift[8] = true;
            }
        }

        private void initModifier(TextBox sender)
        {
            if (sender.Equals(textBox2))
                {
                    MyGlobals.ctrl[0] = false;
                    MyGlobals.alt[0] = false;
                    MyGlobals.shift[0] = false;
                }
                else if (sender.Equals(textBox8))
                {
                    MyGlobals.ctrl[1] = false;
                    MyGlobals.alt[1] = false;
                    MyGlobals.shift[1] = false;
                }
                else if (sender.Equals(textBox10))
                {
                    MyGlobals.ctrl[2] = false;
                    MyGlobals.alt[2] = false;
                    MyGlobals.shift[2] = false;
                }
                else if (sender.Equals(textBox11))
                {
                    MyGlobals.ctrl[3] = false;
                    MyGlobals.alt[3] = false;
                    MyGlobals.shift[3] = false;
                }
                else if (sender.Equals(textBox12))
                {
                    MyGlobals.ctrl[4] = false;
                    MyGlobals.alt[4] = false;
                    MyGlobals.shift[4] = false;
                }
                else if (sender.Equals(textBox13))
                {
                    MyGlobals.ctrl[5] = false;
                    MyGlobals.alt[5] = false;
                    MyGlobals.shift[5] = false;
                }
                else if (sender.Equals(textBox14))
                {
                    MyGlobals.ctrl[6] = false;
                    MyGlobals.alt[6] = false;
                    MyGlobals.shift[6] = false;
                }
                else if (sender.Equals(textBox15))
                {
                    MyGlobals.ctrl[7] = false;
                    MyGlobals.alt[7] = false;
                    MyGlobals.shift[7] = false;
                }
                else if (sender.Equals(textBox16))
                {
                    MyGlobals.ctrl[8] = false;
                    MyGlobals.alt[8] = false;
                    MyGlobals.shift[8] = false;
                }


        }

        private void KeyDownFilter(object sender, KeyEventArgs e)
        {
            /*
            byte[] keys = new byte[106];
            int cp = 0;
            Keyboard.GetKeyboardState(keys);

            for (int i = 0; i < 106; i++)
            {
                if (keys[i] == 1)
                    cp++;
            }
             * 
             * */
            int keyCode1 = (int)e.KeyData;
            int keyCode = keyCode1 & 0xFFFF;

            //MessageBox.Show(e.KeyData.ToString() + " " + keyCode + " " + keyCode1);

            if ((e.Shift || e.Control || e.Alt) && keyCode >= 48 && keyCode <= 90)
            {
                ((TextBox)sender).Clear();

                initModifier((TextBox)sender);

                if (e.Shift)
                    handleShift((TextBox)sender);
                else if (e.Alt)
                    handleAlt((TextBox)sender);
                else if (e.Control)
                    handleCtrl((TextBox)sender);

                if (sender.Equals(textBox2))
                {
                    MyGlobals.tabKey[0] = Convert.ToSByte(e.KeyValue.ToString());
                    //Console.WriteLine(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 0);
                }
                else if (sender.Equals(textBox8))
                {
                    MyGlobals.tabKey[1] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 1);
                }
                else if (sender.Equals(textBox10))
                {
                    MyGlobals.tabKey[2] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 2);
                }
                else if (sender.Equals(textBox11))
                {
                    MyGlobals.tabKey[3] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 3);
                }
                else if (sender.Equals(textBox12))
                {
                    MyGlobals.tabKey[4] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 4);
                }
                else if (sender.Equals(textBox13))
                {
                    MyGlobals.tabKey[5] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 5);
                }
                else if (sender.Equals(textBox14))
                {
                    MyGlobals.tabKey[6] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 6);
                }
                else if (sender.Equals(textBox15))
                {
                    MyGlobals.tabKey[7] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 7);
                }
                else if (sender.Equals(textBox16))
                {
                    MyGlobals.tabKey[8] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e, 8);
                }


            }

            else if (e.KeyValue < 91 && !(e.Shift || e.Control || e.Alt))
            {
                ((TextBox)sender).Clear();

                initModifier((TextBox)sender);

                if (sender.Equals(textBox2))
                {
                    MyGlobals.tabKey[0] = Convert.ToSByte(e.KeyValue.ToString());
                    //Console.WriteLine(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox8))
                {
                    MyGlobals.tabKey[1] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox10))
                {
                    MyGlobals.tabKey[2] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox11))
                {
                    MyGlobals.tabKey[3] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox12))
                {
                    MyGlobals.tabKey[4] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox13))
                {
                    MyGlobals.tabKey[5] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox14))
                {
                    MyGlobals.tabKey[6] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox15))
                {
                    MyGlobals.tabKey[7] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
                else if (sender.Equals(textBox16))
                {
                    MyGlobals.tabKey[8] = Convert.ToSByte(e.KeyValue.ToString());
                    ReplaceDirectionnalArrow((TextBox)sender, e);
                }
            }

            else if (e.Shift || e.Control || e.Alt) ;

            else
            {
                MessageBox.Show("Invalid Key");
                ((TextBox)sender).Text = "";
                //initModifier((TextBox)sender);
            }
                 
        }

        private void ReplaceDirectionnalArrow(TextBox sender, KeyEventArgs e, int id)
        {
            int keyCode = (int)(e.KeyData+32);
            keyCode = keyCode & 0xFFFF;
            
            
            if (MyGlobals.ctrl[id])
                sender.Text = "ctrl + " + ((char)keyCode);
            else if (MyGlobals.alt[id])
                sender.Text = "alt + " + ((char)keyCode);
            else if (MyGlobals.shift[id])
                sender.Text = "shift + " + ((char)keyCode);
            
            
        }

        private void ReplaceDirectionnalArrow(TextBox sender, KeyEventArgs e)
        {
            if (e.KeyValue < 48)
                sender.Text = e.KeyData.ToString();
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //radioButton2.Checked = false;
            //radioButton1.Checked = true;
            MyGlobals.DirectionnalArrowMode = false;
            MyGlobals.PadMode = true;
            ResetLabelGesture();
            resetGestureTab();
            label1.Focus();
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            //radioButton3.Checked = !radioButton3.Checked;
            //MyGlobals.MouseMode = !MyGlobals.MouseMode;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            //radioButton1.Checked = false;
            //radioButton2.Checked = true;
            MyGlobals.DirectionnalArrowMode = true;
            MyGlobals.PadMode = false;
            ResetLabelGesture();
            resetGestureTab();
            label1.Focus();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                MyGlobals.MouseMode = true;
            else
                MyGlobals.MouseMode = false;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            MyGlobals.processOn = false;
            
        }

        private void resetGestureTab()
        {
            for (int i = 0; i < 9; i++)
                MyGlobals.gestureSet[i] = false;
        }

    }
}
